/******************************************************************************
 * Author:      671643387
 * Created:     2016/9/27
 *****************************************************************************/
var mongodb = require('mongodb');
global.config   = require(require('fs').existsSync('../config.js') ? '../config.js' : './config.js');
var logger  = require('./logger.js');
var common  = require('./common.js');
var Mahjong = require('./mahjong/mahjong.js').Mahjong;

function main() {
    logger.init();

    var mongoServer = new mongodb.Server(config.MongodbHost, config.MongodbPort,
        {auto_reconnect:true, poolSize:4});
    var db = new mongodb.Db(config.MongodbName, mongoServer,
        {'native_parser':false, 'w':1, 'wtimeout':2, 'fsync':true});

    db.open(function(err, db) {
        if( err ) {
            ERROR('db open err!');
            process.exit(-1);
        }

        var loader = new common.Loader(function() {
            LOG('DB CREATED');
            process.exit(0);
        });

        loader.addLoad('empty');

        // 创建user数据库
        loader.addLoad('world');
        db.createCollection('world', {}, function(err, result){
            global.gDBWorld = db.collection('world');

            loader.addLoad('mahjong')
            Mahjong.create(function(){
                loader.onLoad('mahjong');
            });

            loader.onLoad('world');
        });

        loader.onLoad('empty');
    });
}
main();